import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";
import {ElMessage} from "element-plus";
import storage from "../utils/storage"

const routes = [
    {
        path: '/',
        redirect: '/dashboard'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '系统首页'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
            },
            {
                path: "/teacher",
                name: "teacher",
                meta: {
                    title: 'teacher'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/teacher.vue")
            },
            {
                path: "/assessment",
                name: "assessment",
                meta: {
                    title: 'assessment'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/assessment.vue")
            },
            {
                path: "/student",
                name: "student",
                meta: {
                    title: 'student'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/student.vue")
            },
            {
                path: "/dormitory",
                name: "dormitory",
                meta: {
                    title: 'dormitory'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/dormitory.vue")
            },
            {
                path: "/special",
                name: "special",
                meta: {
                    title: 'special'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/special_student.vue")
            },
            {
                path: "/poor",
                name: "poor",
                meta: {
                    title: 'poor'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/poor_student.vue")
            },
            {
                path: "/part",
                name: "part",
                meta: {
                    title: 'part'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/partMember.vue")
            },
            {
                path: "/classMeeting",
                name: "classMeeting",
                meta: {
                    title: 'classMeeting'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/classMetting.vue")
            },
            {
                path: "/attendance",
                name: "attendance",
                meta: {
                    title: 'attendance'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/attendance.vue")
            },
            {
                path: "/role",
                name: "role",
                meta: {
                    title: 'role'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/role.vue")
            },
            {
                path: "/User_root",
                name: "User_root",
                meta: {
                    title: 'User_root'
                },
                component: () => import ( /* webpackChunkName: "table" */ "../views/User_Management.vue")
            },
            {
                path: "/charts",
                name: "basecharts",
                meta: {
                    title: '图表'
                },
                component: () => import ( /* webpackChunkName: "charts" */ "../views/BaseCharts.vue")
            },
            {
                path: "/form",
                name: "baseform",
                meta: {
                    title: '表单'
                },
                component: () => import ( /* webpackChunkName: "form" */ "../views/BaseForm.vue")
            }, {
                path: "/tabs",
                name: "tabs",
                meta: {
                    title: 'tab标签'
                },
                component: () => import ( /* webpackChunkName: "tabs" */ "../views/Tabs.vue")
            },  {
                path: "/permission",
                name: "permission",
                meta: {
                    title: '权限管理',
                    permission: true
                },
                component: () => import ( /* webpackChunkName: "permission" */ "../views/Permission.vue")
            }, {
                path: "/i18n",
                name: "i18n",
                meta: {
                    title: '国际化语言'
                },
                component: () => import ( /* webpackChunkName: "i18n" */ "../views/I18n.vue")
            }, {
                path: "/upload",
                name: "upload",
                meta: {
                    title: '上传插件'
                },
                component: () => import ( /* webpackChunkName: "upload" */ "../views/Upload.vue")
            }, {
                path: "/icon",
                name: "icon",
                meta: {
                    title: '自定义图标'
                },
                component: () => import ( /* webpackChunkName: "icon" */ "../views/Icon.vue")
            }, {
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }, {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/User.vue')
            }, {
                path: '/editor',
                name: 'editor',
                meta: {
                    title: '富文本编辑器'
                },
                component: () => import (/* webpackChunkName: "editor" */ '../views/Editor.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});
//登录权限拦截器
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | manage-system`;
    const role = localStorage.getItem('ms_username');
    const power = +(localStorage.getItem("ms_power") || 4);
    const id = localStorage.getItem('ms_id');
    const isLogin = storage.get("time") ? true:false;
    if (!role && to.path == '/login') {
        next();
    }
    else {
        console.log("storage = ", isLogin);
        isLogin?next():next('/login');
    }
});

export default router;
